package com.link.base.base.expenseapplication.service;

import com.link.base.base.expenseapplication.dao.mybatis.mapper.ApplicationProductMapper;
import com.link.base.base.expenseapplication.dao.mybatis.mapper.ApplicationStoreMapper;
import com.link.base.base.expenseapplication.model.ApplicationProductDTO;
import com.link.base.base.expenseapplication.model.ApplicationStore;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 费用申请-门店-费用申请-门店表
 *
 * @author 黄正杨
 * @date 2019/04/16 19:06
 */
@Service
public class ApplicationStoreServiceImpl extends BasicServiceImpl<ApplicationStore> implements ApplicationStoreService {

    @Resource
    private ApplicationStoreMapper applicationStoreMapper;

    @Resource
    private ApplicationProductMapper applicationProductMapper;

    @Override
    public BasicMapper<ApplicationStore> getBasicMapper() {
        return applicationStoreMapper;
    }

    @Override
    public List<ApplicationStore> queryByExamplePage(ApplicationStore entity) throws Exception {
        List<ApplicationStore> list = super.queryByExamplePage(entity);
        if (CollectionUtils.isEmpty(list)) {
            return list;
        }
        List<Long> ids = new ArrayList<>();
        list.forEach(item -> ids.add(item.getId()));
        Map<Long, ApplicationStore> computeMoneyMap = computeMoneyMap(ids);
        ApplicationStore applicationStore;
        for (ApplicationStore item : list) {
            applicationStore = computeMoneyMap.get(item.getId());
            if (null == applicationStore) {
                continue;
            }
            item.setCompanyAmount(applicationStore.getCompanyAmount());
            item.setTotalAmount(applicationStore.getTotalAmount());
            item.setAccountAmount(applicationStore.getAccountAmount());
        }
        return list;
    }

    /**
     * 查询费用申请-门店，并计算汇总金额
     *
     * @author 朱泊阳
     * @date 2019/04/18
     * @param entity
     * @return List
     * @throws Exception
     */
    @Override
    public ApplicationStore queryById(ApplicationStore entity) throws Exception {
        List<Long> ids = new ArrayList<>();
        ApplicationStore item = super.queryById(entity);
        if (null != item) {
            ids.add(item.getId());
            Map<Long, ApplicationStore> computeMoneyMap = computeMoneyMap(ids);
            ApplicationStore applicationStore = computeMoneyMap.get(item.getId());
            item.setCompanyAmount(applicationStore.getCompanyAmount());
            item.setTotalAmount(applicationStore.getTotalAmount());
            item.setAccountAmount(applicationStore.getAccountAmount());
            return item;
        } else {
            return item;
        }
    }

    @Override
    public Map<Long, ApplicationStore> computeMoneyMap(List<Long> ids) throws Exception {
        Map<Long, ApplicationStore> map = new HashMap<>();
        if (CollectionUtils.isEmpty(ids)) {
            return map;
        }
        ApplicationStore applicationStore;
        for (Long str : ids) {
            //初始化头对象，并放进map里方便后面汇总
            applicationStore = new ApplicationStore();
            applicationStore.setId(str);
            applicationStore.setTotalAmount(BigDecimal.ZERO);
            applicationStore.setCompanyAmount(BigDecimal.ZERO);
            applicationStore.setAccountAmount(BigDecimal.ZERO);
            map.put(str, applicationStore);
        }
        List<ApplicationProductDTO> dtoList = applicationProductMapper.queryByChargeIds(ids);
        if (CollectionUtils.isEmpty(dtoList)) {
            return map;
        }
        BigDecimal productPrice; //单价
        BigDecimal amount;//数量
        BigDecimal totalAmount;//申请总额
        BigDecimal accountAmount;//经销商承担金额
        BigDecimal companyAmount;//公司支持金额
        for (ApplicationProductDTO item : dtoList) {
            //单价如果为空则取0
            productPrice = null == item.getProductPrice() ? BigDecimal.ZERO : item.getProductPrice();
            //数量如果为空则取0
            amount = null == item.getAmount() ? BigDecimal.ZERO : new BigDecimal(item.getAmount());
            //申请总额 = 单价*数量
            totalAmount = productPrice.multiply(amount);
            //经销商承担金额计算逻辑
            if (null != item.getApplyPrice()) {
                accountAmount = item.getApplyPrice().multiply(amount);
            } else if (null != item.getApplyDiscount()) {
                accountAmount = totalAmount.multiply(item.getApplyDiscount());
            } else {
                accountAmount = totalAmount;
            }
            //公司支持金额 = 申请总额 - 经销商承担金额
            companyAmount = totalAmount.subtract(accountAmount);
            //汇总头上的金额字段
            applicationStore = map.get(item.getChargeId());
            applicationStore.setAccountAmount(applicationStore.getAccountAmount().add(accountAmount));
            applicationStore.setTotalAmount(applicationStore.getTotalAmount().add(totalAmount));
            applicationStore.setCompanyAmount(applicationStore.getCompanyAmount().add(companyAmount));
            map.put(applicationStore.getId(), applicationStore);
        }
        return map;
    }
}

